package org.lonetree.tc.webapp.actions;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book;
import org.lonetree.tc.core.Book.Isbn;
import org.lonetree.tc.core.dao.BookDAO;
import org.lonetree.tc.core.dao.DAOFactory;
import org.lonetree.tc.webapp.forms.Temp;

public class ViewBookSections extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String dummy=(request.getParameter("isbn"));
		PrintWriter out=response.getWriter();
		out.println("ok");
		String dummy2=dummy;
		try {
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			BookDAO bdao = factory.getBookDAO();
			Book book=bdao.getBook(new Isbn(request.getParameter("isbn")));
//			int isbn=Integer.parseInt(request.getParameter("isbn"));
//			Class.forName("com.mysql.jdbc.Driver");
//			Connection connection  = DriverManager.getConnection(
//					"jdbc:mysql://localhost:3306/tc", "tc",
//					"textbookcrossroads");
//			String query1 ="select C.course_title, C.id as course_id, S.id as section_id, S.section_num as section_number, S.professor ";
//			String query2 ="from textbooks as B,courses as C, sections as S, section_textbooks as ST ";
//			String query3 ="where ST.textbook_id=B.id and C.id=S.ourse_id and ST.section_id=S.id and B.isbn13="+isbn+"";
//			String query=query1+query2+query3;
//			Statement statement = connection.createStatement();
//			ResultSet rs=statement.executeQuery(query);
//			ArrayList<Temp> booksections=new ArrayList<Temp>();
//			Temp a=new Temp("test",1,1,1,"hello");
//			booksections.add(a);
//			while (rs.next())
//			{
//				Temp record=new Temp(rs.getString("course_title"),rs.getInt("course_id"),
//						rs.getInt("section_id"),rs.getInt("section_num"),rs.getString("professor"));
//				booksections.add(record);
//			}
//			request.setAttribute("booksections", booksections);
			request.setAttribute("test", book);
//			rs.close();
//			statement.close();
//			connection.close();

			return mapping.findForward("success");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			return mapping.findForward("failure");
		}
	}

}
